//
//  SYCollectionDetailsVC.m
//  TermOfSectionB
//
//  Created by 韩震明 on 15/11/29.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "SYCollectionDetailsVC.h"

@interface SYCollectionDetailsVC ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain) NSMutableArray *dataArr;
@property(nonatomic, retain) NSMutableArray *productArr;
@property(nonatomic, retain) SYBaseCollectionView *collection;

@end

@implementation SYCollectionDetailsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavigation];
    
    [self createCollection];
    
    [self createCDData];
    
    // 可以写上下刷新, 还有hud等待, 头部可以加collecView联动
}

#pragma mark - CollectionView Setting

- (void)createCollection
{
    SYBaseCollectionViewFlowLayout *flowLayout = [[SYBaseCollectionViewFlowLayout alloc] initWithItemSize:CGSizeMake((self.view.frame.size.width - 10) / 2, self.view.frame.size.height / 3) scrollDirection:UICollectionViewScrollDirectionVertical sectionInsert:UIEdgeInsetsMake(5, 0, 0, 0)];
    
    self.collection = [[SYBaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 64) collectionViewLayout:flowLayout backgroundColor:[UIColor whiteColor] superView:self.view showsHorizontalScrollIndicator:NO showsVerticalScrollIndicator:YES delegate:self];
    
    [self.collection makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.and.right.equalTo(self.view);
    }];
    
    [self.collection registerClass:[SYGoodProductCVCell class] forCellWithReuseIdentifier:@"collectionReuse"];
}

#pragma mark - CollectionView Delegate Method

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.productArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SYGoodProductCVCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"collectionReuse" forIndexPath:indexPath];
    cell.productModel = self.productArr[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SYBaseWebView *itemVC = [[SYBaseWebView alloc] init];
    [self.navigationController pushViewController:itemVC animated:YES];
    itemVC.WebViewUrlStr = [self.productArr[indexPath.row] taobaourl];
    itemVC.titleForWeb = @"宝贝详情";
}

#pragma mark - Navigation Setting

- (void)setNavigation
{
    self.navigationItem.title = @"精选好物";
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
}

#pragma mark - Navigationbar ButtonAction

- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Internet Data

- (void)createCDData
{
    NSDictionary *topBody = [NSDictionary dictionaryWithObjectsAndKeys:@"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", @"bt_app_ios", @"client_id", nil];
    NSDictionary *ViewBody = [NSDictionary dictionaryWithObjectsAndKeys:@"bt_app_ios", @"client_id", @"9c1e6634ce1c5098e056628cd66a17a5", @"client_secret", nil];
    
    [AFN POSTDataWithURL:@"http://open3.bantangapp.com/category/list" POST:topBody block:^(id result) {
        NSDictionary *dic = result;
        self.dataArr = [NSMutableArray array];
        self.dataArr = [SYGoodProductTopModel baseModelByArr:dic[@"data"]];
    }];
    
    [AFN POSTDataWithURL:@"http://open3.bantangapp.com/product/list" POST:ViewBody block:^(id result) {
        NSDictionary *dic = result;
        self.productArr = [NSMutableArray array];
        self.productArr = [SYGoodProductModel baseModelByArr:dic[@"data"][@"product"]];
        [self.collection reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
